package com.ruoyi.report_sh.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_tg.domain.TgsybgBaDetail;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.report_tg.mapper.TgsybgBaDetailMapper;
import com.ruoyi.sycs.service.IBiQualificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sh.domain.bo.ReportShShsybgBsInfoBo;
import com.ruoyi.report_sh.domain.vo.ReportShShsybgBsInfoVo;
import com.ruoyi.report_sh.domain.ReportShShsybgBsInfo;
import com.ruoyi.report_sh.mapper.ReportShShsybgBsInfoMapper;
import com.ruoyi.report_sh.service.IReportShShsybgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 石灰试验检测报告Service业务层处理
 *
 * @author llb
 * @date 2024-09-04
 */
@RequiredArgsConstructor
@Service
public class ReportShShsybgBsInfoServiceImpl implements IReportShShsybgBsInfoService {

    private final ReportShShsybgBsInfoMapper baseMapper;
    private final TgsybgBaDetailMapper detailMapper;

    private final IBiQualificationService biQualificationService;

    private static final Long[] keys = {85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L};

    /**
     * 查询石灰试验检测报告
     */
    @Override
    @FIllBasicInfo
    public ReportShShsybgBsInfoVo queryById(Long excelId){
        ReportShShsybgBsInfoVo reportShShsybgBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<TgsybgBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<TgsybgBaDetail>()
            .eq(TgsybgBaDetail::getExcelId, excelId));
        baDetailVos.forEach(v->{
            v.setQualInfo(biQualificationService.queryById(v.getQualId()));
        });
        // 格式化 技术指标
        biQualificationService.compute(baDetailVos);
        // 转换成map形式
        Map<Long, TgsybgBaDetailVo> collect = baDetailVos.stream().collect(Collectors.toMap(TgsybgBaDetailVo::getQualId, v -> {
            return v;
        }));
        reportShShsybgBsInfoVo.setBaDetailVos(collect);
        return reportShShsybgBsInfoVo;
    }

    /**
     * 查询石灰试验检测报告列表
     */
    @Override
    public TableDataInfo<ReportShShsybgBsInfoVo> queryPageList(ReportShShsybgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportShShsybgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportShShsybgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询石灰试验检测报告列表
     */
    @Override
    public List<ReportShShsybgBsInfoVo> queryList(ReportShShsybgBsInfoBo bo) {
        LambdaQueryWrapper<ReportShShsybgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportShShsybgBsInfo> buildQueryWrapper(ReportShShsybgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportShShsybgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportShShsybgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportShShsybgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportShShsybgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), ReportShShsybgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), ReportShShsybgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportShShsybgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportShShsybgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportShShsybgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportShShsybgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportShShsybgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportShShsybgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportShShsybgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportShShsybgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportShShsybgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportShShsybgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGuigxh()), ReportShShsybgBsInfo::getGuigxh, bo.getGuigxh());
        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), ReportShShsybgBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getJincrq()), ReportShShsybgBsInfo::getJincrq, bo.getJincrq());
        lqw.eq(StringUtils.isNotBlank(bo.getPih()), ReportShShsybgBsInfo::getPih, bo.getPih());
        lqw.eq(StringUtils.isNotBlank(bo.getChucrq()), ReportShShsybgBsInfo::getChucrq, bo.getChucrq());
        lqw.eq(StringUtils.isNotBlank(bo.getCd()), ReportShShsybgBsInfo::getCd, bo.getCd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportShShsybgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportShShsybgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportShShsybgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportShShsybgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportShShsybgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportShShsybgBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), ReportShShsybgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportShShsybgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportShShsybgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportShShsybgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增石灰试验检测报告
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportShShsybgBsInfoBo bo) {
        ReportShShsybgBsInfo add = BeanUtil.toBean(bo, ReportShShsybgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 多个keys
            ArrayList<TgsybgBaDetail> details = new ArrayList<>(8);
            for (Long key : keys) {
                details.add(new TgsybgBaDetail(add.getExcelId(), key));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改石灰试验检测报告
     */
    @Override
    @DSTransactional
    @ComputeReportResult
    public Boolean updateByBo(ReportShShsybgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        ReportShShsybgBsInfo update = BeanUtil.toBean(bo, ReportShShsybgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos().values(), TgsybgBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(ReportShShsybgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
            for (TgsybgBaDetailVo detailVo : bo.getBaDetailVos().values()) {
                if ("不符合".equals(detailVo.getJiegpd())) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
        }
        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportShShsybgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除石灰试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public ReportShShsybgBsInfoVo jcjl(ReportShShsybgBsInfoVo vo) {
        StringBuilder result = new StringBuilder("检测结论：经检测，该石灰");
        boolean haveShicz = false;
        for (TgsybgBaDetailVo detailVo : vo.getBaDetailVos().values()) {
            if ("实测值".equals(detailVo.getJiegpd())) {
                result.append(detailVo.getQualName()).append("为").append(detailVo.getJiancjg()).append(detailVo.getQualUnit()).append("，");
                haveShicz = true;
            }
        }
        if (haveShicz) {
            result.append("其他指标符合");
        } else {
            result.append("所检指标符合");
        }
        result.append("《大粒径透水性沥青混合料应用技术规范》DB37/T 1161-2009及设计文件相关技术要求。");
        vo.setJiancjl(result.toString());
        return vo;
    }
}
